package com.zxm.web.controller.txcloud.face;

import com.tencentcloudapi.fmu.v20191213.models.*;
import com.zxm.common.core.domain.AjaxResult;
import com.zxm.common.core.domain.TxyFactory;
import com.zxm.common.core.domain.entity.TxyFaceShiZhuang;
import com.zxm.common.core.redis.RedisCache;
import com.zxm.common.exception.NeedPaidException;
import com.zxm.common.utils.StringUtils;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.fmu.v20191213.FmuClient;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

/**
 * @Author silence.Z
 * @Date 2022/6/14 14:35
 * @PackageName:com.zxm.web.controller.txcloud.face
 * @ClassName: zxmController
 * @Description: 人脸试装控制器
 * @Version 1.0
 */
@RestController
@RequestMapping("txcloud/face/faceshizhuang")
public class FaceShiZhuangController {
	private static final Logger log = LoggerFactory.getLogger(FaceShiZhuangController.class);
	@Autowired
	private RedisCache redisCache;

	@PreAuthorize("@ss.hasPermi('txcloud:face:faceshizhuang:list')")
	@PostMapping("styleImage")
	/**
	 * 图片滤镜
	 */
	public AjaxResult styleImage(@Validated @RequestBody TxyFaceShiZhuang faceShiZhuang) throws TencentCloudSDKException {
		FmuClient client = TxyFactory.createFaceShiZhuang();
		StyleImageRequest req = new StyleImageRequest();
		if (StringUtils.isNotEmpty(Collections.singleton(faceShiZhuang.getFilterType()))) {
			req.setFilterType(faceShiZhuang.getFilterType());
		}
		if (StringUtils.isNotEmpty(faceShiZhuang.getImage())) {
			req.setImage(faceShiZhuang.getImage());
		}
		if (StringUtils.isNotEmpty(faceShiZhuang.getUrl())) {
			req.setUrl(faceShiZhuang.getUrl());
		}
		if (StringUtils.isNotEmpty(Collections.singleton(faceShiZhuang.getFilterDegree()))) {
			req.setFilterDegree(faceShiZhuang.getFilterDegree());
		}
		req.setRspImgType("url");
		StyleImageResponse resp = client.StyleImage(req);
		// 输出json格式的字符串回包
		log.info(StyleImageResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}

	@PreAuthorize("@ss.hasPermi('txcloud:face:faceshizhuang:list')")
	@PostMapping("styleImagePro")
	/**
	 * 图片滤镜(高级版)
	 */
	public AjaxResult styleImagePro(@Validated @RequestBody TxyFaceShiZhuang faceShiZhuang) throws TencentCloudSDKException {
		FmuClient client = TxyFactory.createFaceShiZhuang();
		StyleImageProRequest req = new StyleImageProRequest();
		if (StringUtils.isNotEmpty(Collections.singleton(faceShiZhuang.getFilterType()))) {
			req.setFilterType(faceShiZhuang.getFilterType());
		}
		if (StringUtils.isNotEmpty(faceShiZhuang.getImage())) {
			req.setImage(faceShiZhuang.getImage());
		}
		if (StringUtils.isNotEmpty(faceShiZhuang.getUrl())) {
			req.setUrl(faceShiZhuang.getUrl());
		}
		if (StringUtils.isNotEmpty(Collections.singleton(faceShiZhuang.getFilterDegree()))) {
			req.setFilterDegree(faceShiZhuang.getFilterDegree());
		}
		req.setRspImgType("url");
		StyleImageProResponse resp = client.StyleImagePro(req);
		// 输出json格式的字符串回包
		log.info(StyleImageProResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}

	/**
	 * 人脸美颜（1000次内免费）
	 */
	@PreAuthorize("@ss.hasPermi('txcloud:face:faceshizhuang:list')")
	@PostMapping("beautifyPic")
	public AjaxResult BeautifyPic(@Validated @RequestBody TxyFaceShiZhuang faceShiZhuang) throws TencentCloudSDKException {
		//每月免费额度1000
		int num = ObjectUtils.isEmpty(redisCache.getCacheObject("beautifyPic")) ? 0 : Integer.parseInt(redisCache.getCacheObject("beautifyPic").toString());
		if (num >= 1000) {
			throw new NeedPaidException();
		}
		FmuClient client = TxyFactory.createFaceShiZhuang();
		BeautifyPicRequest req = new BeautifyPicRequest();

		req.setWhitening(faceShiZhuang.getWhitening());
		req.setSmoothing(faceShiZhuang.getSmoothing());
		req.setFaceLifting(faceShiZhuang.getFaceLifting());
		req.setEyeEnlarging(faceShiZhuang.getEyeEnlarging());
		if (StringUtils.isNotEmpty(faceShiZhuang.getImage())) {
			req.setImage(faceShiZhuang.getImage());
		}
		if (StringUtils.isNotEmpty(faceShiZhuang.getUrl())) {
			req.setUrl(faceShiZhuang.getUrl());
		}

		req.setRspImgType("url");
		BeautifyPicResponse resp = client.BeautifyPic(req);
		//请求次数加1 设置过期时间为下月初减当前时间秒值
		LocalDateTime midnight = LocalDateTime.now().plusMonths(1).withDayOfMonth(1).withHour(0)
				.withMinute(0).withSecond(0).withNano(0);
		redisCache.setCacheObject("beautifyPic", ++num, Math.toIntExact(ChronoUnit.SECONDS.between(LocalDateTime.now(), midnight)), TimeUnit.SECONDS);
		// 输出json格式的字符串回包
		log.info(StyleImageProResponse.toJsonString(resp));
		return AjaxResult.success(resp);
	}

}
